#version 430

in vec3 normal;
in vec4 position;
in vec3 eye_normal;
in vec4 eye_position;
in vec4 color;


uniform sampler2D layer_color;
uniform sampler2D layer_aux0;
uniform sampler2D layer_depth ;
uniform sampler2D layer_normal ;

uniform mat4 p3d_ProjectionMatrix;
uniform vec2 texpad;

out vec4 mygl_FragData_0;

vec4 alphaBlend(vec4 top, vec4 bottom)
{
    vec3 color = (top.rgb * top.a) + (bottom.rgb * (1 - top.a));
    float alpha = top.a + bottom.a * (1 - top.a);

    return vec4(color, alpha);
}

void main()
     
 {
    vec2 p = gl_FragCoord.xy / texpad;
    vec4 color =texture2D(layer_color, p);
    vec4 glass =texture2D(layer_aux0, p);
    float v = glass.r;
    float v2 = glass.g;
    float v3 = glass.b;
    //~ glass.rgb = vec3(0.9, 0.9, 0.9);
    glass.rgb = vec3(1, 1, 1);      // 'glass' is light
    glass.a = v;
    
    vec3 black = vec3(0.05, 0.05, 0.05);
    vec4 gblack = v3 * vec4(black, mix(0.4, 0.1, v2));
    
    
    //glass = alphaBlend(glass, gblack);
    
    vec4 result = alphaBlend(glass, alphaBlend(gblack, color));
    //~ vec4 result = alphaBlend(gblack, color);
    //~ vec4 result = vec4(glass.a, 0, 0, 1);
    
    glass = clamp(glass, 0, 1);
    mygl_FragData_0 = result;
}



